// _Exercise1_GreyScale_Histogram.cpp : Defines the entry point for the console application.
//

// Chua xu ly truong hop bi tran ra ngoai

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "stdio.h"

void myMouse_callback(int event,int x,int y,int flags, void* params);

CvRect box;
IplImage* origin;
bool drawing_box = false;

void DrawBox(IplImage* img, CvRect rect)
{
	cvRectangle(img,cvPoint(box.x,box.y),cvPoint(box.x+box.width, box.y+box.height),cvScalar(0xff,0x00,0x00));	
}


void myMouse_callback(int event,int x,int y,int flags, void* params)
{
	IplImage* image = (IplImage*)params;
	switch(event)
	{
	case CV_EVENT_LBUTTONDOWN:
		{
			//cvZero(image);
			cvCopyImage(origin,image);
			drawing_box = true;
			box = cvRect(x,y,0,0);
		}
		break;
	case CV_EVENT_MOUSEMOVE:
		{
			if(drawing_box)
			{
				box.width = x-box.x;
				box.height = y - box.y;
			}
		}
		break;
	case CV_EVENT_LBUTTONUP:
		{
			drawing_box = false;
			if(box.width < 0)
			{
				box.x += box.width;
				box.width = -box.width;
			}
			if(box.height < 0)
			{
				box.y += box.height;
				box.height = -box.height;
			}
			DrawBox(image,box);
		}
		break;
	}
}

void myGreyScale(IplImage* src,IplImage* desc)
{
	uchar* data = (uchar*) src->imageData;	
	if(!desc)
		desc = cvCreateImage(cvSize(src->width,src->height),src->depth,src->nChannels);
	uchar* greydata = (uchar*) desc->imageData;

	for(int i = 0; i < src->height; i++)
		for(int j = 0; j <  src->width; j++)
		{
			int index = i*src->widthStep + j * src->nChannels;
			double t = data[index + 2]*0.299 + data[index + 1] *0.587 + data[index]*0.114;
			greydata[index] = greydata[index+1] = greydata[index+2] = (uchar)t;
		}	
}



int _tmain(int argc, _TCHAR* argv[])
{
	box = cvRect(-1,-1,0,0);

	char path[256];
	//	path="D:\\Field.jpg";

	// create image
	//	printf("Plz iinput path of image");
	//	scanf("%s",path);

	IplImage* image = cvLoadImage("D:\\Field.jpg");

	IplImage* temp = cvCloneImage(image);
	origin = cvCloneImage(image);

	cvNamedWindow("Main_Window");
	cvNamedWindow("Histogram");

	// create for histogram
	IplImage* img_histogram=0;
	CvHistogram* histogram=0;

	int bins = 256;
	int hsize[] = {bins};

	float max_value = 0, min_value = 0;

	float value;
	int normalized;

	float xranges[] = { 0, 256 };
	float* ranges[] = { xranges };


	cvSetMouseCallback("Main_Window",myMouse_callback,(void*)image);

	while(1)
	{
		cvCopyImage(image,temp);
		if(drawing_box)
		{
			DrawBox(temp,box);
		}
		if(box.height > 0 && box.width > 0)
		{
			IplImage* sub_img = cvCreateImageHeader(cvSize(box.width,box.height),temp->depth,temp->nChannels);
			sub_img->origin = temp->origin;
			sub_img->widthStep = temp->widthStep;
			sub_img->imageData = temp->imageData + box.y*temp->widthStep + box.x*temp->nChannels;

			myGreyScale(sub_img,sub_img);

			IplImage* r_plane = cvCreateImage(cvGetSize(sub_img),8,1);
			IplImage* g_plane = cvCreateImage(cvGetSize(sub_img),8,1);
			IplImage* b_plane = cvCreateImage(cvGetSize(sub_img),8,1);
			cvCvtPixToPlane(sub_img,r_plane,b_plane,g_plane,0);

			IplImage* planes[] = { r_plane };
			histogram = cvCreateHist(1,hsize,CV_HIST_ARRAY,ranges,1);

			cvCalcHist( planes, histogram, 0, NULL);
			cvGetMinMaxHistValue( histogram, &min_value, &max_value);
			//			printf("min: %f, max: %f\n", min_value, max_value);

			img_histogram = cvCreateImage(cvSize(bins, 50),8,1);
			cvRectangle(img_histogram, cvPoint(0,0), cvPoint(256,50), CV_RGB(255,255,255),-1);

			for(int i=0; i < bins; i++)
			{
				value = cvQueryHistValue_1D( histogram, i);
				normalized = cvRound(value*50/max_value);
				cvLine(img_histogram,cvPoint(i,50), cvPoint(i,50-normalized), CV_RGB(0,0,0));
			}

			cvShowImage( "Histogram", img_histogram );

			cvReleaseImageHeader(&sub_img);
			cvReleaseImage(&r_plane);
			cvReleaseImage(&b_plane);
			cvReleaseImage(&g_plane);
		}
		cvShowImage("Main_Window",temp);

		if(cvWaitKey(15)==27)
			break;
	}

	cvReleaseImage(&image);
	cvReleaseImage(&temp);
	cvReleaseImage(&origin);
	cvDestroyWindow("Main_Window");
	cvDestroyWindow("Histogram");
	return 0;
}



